package com.example.bgmachine.bgmachine.goods;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.Urlinfor;
import com.example.bgmachine.bgmachine.commons.ImageloadView;
import com.example.bgmachine.bgmachine.commons.NetImageView;
import com.example.bgmachine.bgmachine.homes.HomePages;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class HomepageGridRightMenuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HomepageGridRightMenuItemData> list = new ArrayList<HomepageGridRightMenuItemData>();

    public HomepageGridRightMenuAdapter(Context context, ArrayList<HomepageGridRightMenuItemData> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public HomepageGridRightMenuItemData getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public boolean fileIsExists(String fileName) {
        try {
            // 首先保存图片
            File pathsFile = HomePages.mHomePages.getBaseContext().getDir("users", Context.MODE_PRIVATE);//Environment.getExternalStorageDirectory();//getDataDirectory();// mContext.getDir("users", Context.MODE_PRIVATE);

            File appDir = new File(pathsFile, "temp");
            if (!appDir.exists()) {
                Log.d("aaa", "fileIsExists==1");
                appDir.mkdir();
            }else {
                Log.d("aaa", "fileIsExists==2");
            }

            File f = new File(appDir, fileName);

            if (f.exists()) {

                return true;
            } else {

                return false;
            }
        } catch (Exception e) {
            Log.i("aaa", "崩溃");
            return false;
        }
    }
    public void downloadImage(String imageUrl,String imageFiles) {
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            inputStream = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            File pathsFile = HomePages.mHomePages.getBaseContext().getDir("users", Context.MODE_PRIVATE);//Environment.getExternalStorageDirectory();//getDataDirectory();// mContext.getDir("users", Context.MODE_PRIVATE);

            File appDir = new File(pathsFile, "temp");
            File file = new File(appDir, imageFiles);

            outputStream = new FileOutputStream(file);


            if(imageFiles.contains(".png")){
                Log.d("aaa", "saveToSystemGallery=02");
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                Log.d("aaa", "saveToSystemGallery=03");
            }
            Log.d("aaa", "saveToSystemGallery=1");
            if(imageFiles.contains(".jp")){
                Log.d("aaa", "saveToSystemGallery=04");
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                Log.d("aaa", "saveToSystemGallery=05");
            }
            outputStream.flush();
            outputStream.close();



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            View view = View.inflate(context, R.layout.homepage_grid_right_menu_adapter, null);
            convertView = view;
            TextView homepage_grid_right_menu_titles = (TextView) view.findViewById(R.id.homepage_grid_right_menu_titles);
            ImageloadView homepage_grid_right_menu_image = (ImageloadView) view.findViewById(R.id.homepage_grid_right_menu_image);
            homepage_grid_right_menu_titles.setText(list.get(position).getMenuNames());
            Log.d("aaa", "getView: getMenuNames=1=" + list.get(position).getMenuNames());

            String imagePaths = list.get(position).getImagePaths();
            Log.d("aaa", "getView: imagePaths=0=" + imagePaths);
            if (imagePaths.length() <= 0) {
                homepage_grid_right_menu_image.setImageResource(R.mipmap.homepage_grid__item_image_demos);

            } else {
                Log.d("aaa", "getView: imagePaths=1=" + imagePaths);
                //显示本地图片
                //  Bitmap bitmap = BitmapFactory.decodeFile(list.get(position).getImagePaths());
                //  homepage_grid_right_menu_image.setImageBitmap(bitmap);
                if (true) {
                    String pathsfa =list.get(position).getImagePaths();
                    String pathsa = Urlinfor.getmUrlinfor().homeImage + Urlinfor.getmUrlinfor().mMenuPaths + pathsfa ;
                    Log.d("aaa", "getView: pathsa=1=" + pathsa);
                    Log.d("aaa", "getView: downloadImage=10=" + pathsfa);
                    homepage_grid_right_menu_image.setImageURL(pathsa,pathsfa);

                    //   netImageViews.mImageView = homepage_grid_right_menu_image;
                    //  netImageViews.setImageURL(Urlinfor.getmUrlinfor().homeImage + Urlinfor.getmUrlinfor().mMenuPaths + list.get(position).getImagePaths());
                }
            }
        } else {
            TextView homepage_grid_right_menu_titles = (TextView) convertView.findViewById(R.id.homepage_grid_right_menu_titles);
            ImageloadView homepage_grid_right_menu_image = (ImageloadView) convertView.findViewById(R.id.homepage_grid_right_menu_image);
            homepage_grid_right_menu_titles.setText(list.get(position).getMenuNames());
            Log.d("aaa", "getView: getMenuNames=2=" + list.get(position).getMenuNames());

            String imagePaths = list.get(position).getImagePaths();
            if (imagePaths.length() <= 0) {
                homepage_grid_right_menu_image.setImageResource(R.mipmap.homepage_grid__item_image_demos);

            } else {
                Log.d("aaa", "getView: imagePaths=2=" + imagePaths);


                //显示本地图片
                //   Bitmap bitmap = BitmapFactory.decodeFile(list.get(position).getImagePaths());
                //  homepage_grid_right_menu_image.setImageBitmap(bitmap);
                if (true) {
                    String pathsfa =list.get(position).getImagePaths();
                    String pathsa = Urlinfor.getmUrlinfor().homeImage + Urlinfor.getmUrlinfor().mMenuPaths + pathsfa;
                    Log.d("aaa", "getView: pathsa=2=" + pathsa);
                    Log.d("aaa", "getView: downloadImage=20=" + pathsfa);
                    homepage_grid_right_menu_image.setImageURL(pathsa,pathsfa);

                    // netImageViews.mImageView = homepage_grid_right_menu_image;
                    //netImageViews.setImageURL(Urlinfor.getmUrlinfor().homeImage+ Urlinfor.getmUrlinfor().mMenuPaths + list.get(position).getImagePaths());
                }
            }
        }


        return convertView;
    }
}
